#include "process.h"
#include <string.h>

#define SIZE 101
#define STYLE '#'

//v2，模拟实际下载的进度条效果
void FlushProcess(const char* tips, double total, double current)
{
    char buffer[SIZE];
    memset(buffer, 0, sizeof(buffer));
    double rate = current*100 / total; 
    int num = (int)rate;

    const char* lable = "|/-\\";
    static int index = 0;
    int len = strlen(lable);

    int i = 0;
    for(; i<num; ++i)
        buffer[i] = STYLE;
    printf("%s...[%-100s][%.lf%%][%c]\r", tips, buffer, rate, lable[index++]);
    fflush(stdout);
    index = index % len;

    if(num >= 100)
        printf("\n");
}

//v1：展示进度条的基本功能
void process()
{
    int rate = 0;
    char buffer[SIZE];
    memset(buffer, '\0' , sizeof(buffer));
    const char* lable = "|/-\\";
    int len = strlen(lable);

    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", buffer, rate, lable[rate % len]);
        fflush(stdout);
        buffer[rate] = STYLE;
        ++rate;
        usleep(50000);
    }
    printf("\n");
}
